package com.heu.lu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Car {
	int id;
	double x;
	double y;
	ArrayList<Car> nearbyCars;
	public List<Car> findNextNCars(int n) {
		if (nearbyCars == null || nearbyCars.size() <= n) {
			return new ArrayList<Car>(nearbyCars);
		}

		Collections.sort(nearbyCars, new Comparator<Car>() {

			@Override
			public int compare(Car c1, Car c2) {
				double d1 = Car.getDistanse(c1.x, c2.y, x, y);
				double d2 = Car.getDistanse(c2.x, c2.y, x, y);
				if (d1 > d2) {
					return 1;
				} else if (d1 < d2) {
					return -1;
				}
				
				return 0;
			}

		});

		return nearbyCars.subList(0, n);
	}

	static double getDistanse(double x1, double y1, double x2, double y2) {
		double tx = x1 - x2;
		double ty = y1 - y2;
		double distanse = Math.sqrt(tx*tx + ty*ty);
		return distanse;
	}
}
